<?php

class Socios extends Controller {

	function Socios()
	{
		parent::Controller("socios");
	}

    /**
     * Function to process the Form Data
     */
    function process() {
        $this->load->library("validation");
        $this->validation->set_message('required', 'Obligatorio');
        $this->validation->set_message('valid_email', 'Inv&aacute;lido');
        //set none delimiters
        $this->validation->set_error_delimiters('', '');

        $fields['name'] 			  = 'name';
        $fields['email']    		  = 'email';
        $fields['telephone']    	  = 'telephone';
        $fields['id_plan']  		  = 'id_plan';
        $fields['marker_name'] 		  = 'marker_name';
        $fields['marker_address']     = 'marker_address';
        $fields['marker_city']    	  = 'marker_city';
        $fields['marker_description'] = 'marker_description';        
        $this->validation->set_fields($fields);

        $rules['name'] 			     = 'required';
        $rules['email']    			 = 'required|valid_email';
        $rules['telephone']    	  	 = 'numeric';
        $rules['id_plan'] 			 = 'required';
        $rules['marker_name'] 		 = 'required';
        $rules['marker_address']     = 'required';
        $rules['marker_city']   	 = 'required';
        $rules['marker_description'] = 'required';  
        $this->validation->set_rules($rules);
        if ($this->validation->run() == FALSE) {
        	//there was an error!!!
        	/*
            $data = array(
            		'name' 			     => $this->validation->username_error,
            		'email'    			 => $this->validation->email_error,
            		'telephone'    	  	 => $this->validation->phone_error,
            		'id_plan' 			 => $this->validation->email_error,
            		'marker_name' 		 => $this->validation->email_error,
            		'marker_address'     => $this->validation->email_error,
            		'marker_city'    	 => $this->validation->email_error,
            		'marker_description' => $this->validation->email_error,
                	'error'   			 => true
                );
            echo json_encode($data);
            */
        	$this->index();
        } else {
        	//the data is correct!
        	//do the insert to DB and send mail
        	
            $data = array(
                	'error'   			 => false,
            		'name' 			     => $this->input->post("name"),
            		'email'    			 => $this->input->post("email"),
            		'telephone'    	  	 => $this->input->post("telephone"),
            		'id_plan' 			 => $this->input->post("id_plan"),
            		'marker_name' 		 => $this->input->post("marker_name"),
            		'marker_address'     => $this->input->post("marker_address"),
            		'marker_city'    	 => $this->input->post("marker_city"),
            		'marker_description' => $this->input->post("marker_description")
                );

            $data = json_encode($data);
            $this->email->from('conocepunta@gmail.com');
            $this->email->to('conocepunta@gmail.com');
            $this->email->subject('[Socios]');
            $this->email->message($data);
            $this->email->send();
			
            /*
			//send feedback
			$msg = "Gracias por Asociarte. Validaremos la informacion y nos pondremos en contacto a la brevedad".
				   "No dejes de visitar www.conocepunta.com.ar";					
            $this->email->from('conocepunta@gmail.com');
            $this->email->to($this->input->post("email"));
            $this->email->subject('Gracias por Asociarte!');
            $this->email->message($msg);
            $this->email->send();
            */			
            //echo $data;
        }
    }

	function index()
	{
      $data['page_title'] = "Asociate a ConocePunta y form&aacute; parte del sitio m&aacute;s completo de la Zona!";
      $data['meta_description'] = '<meta name="description" content="ConocePunta - Punta Alta, Buenos Aires, Argentina - Asociate, ten&eacute;s que tener presencia en Internet." />';      
      $output = $this->load->view('head_view', $data, true);
	  $navbar = $this->write_navbar();
      
      $output .= $navbar . "<div class='slim-content centered'><div style='display: inline-block;'>";
            
      $query = $this->db->query("SELECT * FROM plan;");
      $planes = $query->result_array();      
      $plan_select = '<select name="id_plan" id="id_plan" class="input" disabled="disabled">';
      foreach ($planes as $k => $v){
      	$plan_select .= "<option value='".$v['id_plan']."'>".$v['nombre']."</option>";
      }
      $plan_select .= "</select>";
            
      $form = "<div class='lheader contact_side'>".form_open(base_url()."socios/process", 'onsubmit="return false;" id="socio"').
              '<div>
                  <label for="name">*Nombre</label><br>
                  <input type="text" name="name" id="name" value="'.set_value('name').'" class="input regular-text, validate[required]"/>
              </div>
              <div>
                  <label for="email">*Email</label><br>
                  <input type="text" name="email" id="email" value="'.set_value('email').'" class="input regular-text, validate[required, custom[email]]"/>
              </div>
              <div>
                  <label for="telephone">T&eacute;lefono</label><br>
                  <input type="text" name="telephone" id="telephone" value="'.set_value('telephone').'" class="input regular-text, validate[custom[phone]]"/>
              </div>              
              <p class="separator"></p>
              <div>
				<label for="id_plan">Plan Seleccionado</label><br>'.
					$plan_select
			  .'</div>              
              <div>
                  <label for="marker_name">*Nombre del Negocio</label><br>
                  <input type="text" name="marker_name" id="marker_name" value="'.set_value('marker_name').'" class="input regular-text, validate[required]"/>
              </div>
              <div>
                  <label for="marker_address">*Direcci&oacute;n del Negocio</label><br>
                  <input type="text" name="marker_address" id="marker_address" value="'.set_value('marker_address').'" class="input regular-text, validate[required]"/>
              </div>
              <div>
                  <label for="marker_city">Ciudad</label><br>
                  <input disabled="disabled" type="text" name="marker_city" id="marker_city" value="Punta Alta"/>
              </div>                                          
              <div>
                  <label for="marker_description">*Descripcion</label><br>
                  <textarea rows="10" cols="35" name="marker_description" id="marker_description" value="'.set_value('marker_description').'" class="input regular-text, validate[required]"></textarea>
              </div>
              <input class="submit ui-button" type="submit" value="Enviar" />
              <span id="success" class="ui-state-highlight" style="display:none"></span>
              </form>
      		</div>';

      $output .= $form;

      //-------DESCRIPCION DE PLANES------------------------
      $output .= "<div class='rheader contact_side' style='max-width:50%'>".      		
      		"<ul>Gratis".
	      		"<li>Posicion&aacute; tu negocio Online.</li>".
	      		"<li>Agreg&aacute; una descripci&oacute;n breve de tu negocio.</li>".
	      		"<li>Ubicamos tu negocio en el mapa interactivo.</li>".
      		"</ul>".
      		/*
      		"<ul>B&aacute;sico".
	      		"<li>Posicion&aacute; tu negocio Online.</li>".
	      		"<li>Agreg&aacute; una descripci&oacute;n amplia de tu negocio.</li>".
	      		"<li>Ubicamos tu negocio en el mapa interactivo.</li>".
      		"</ul>".
      		"<ul>Medio".
	      		"<li>Posicion&aacute; tu negocio Online.</li>".
	      		"<li>Agreg&aacute; una descripci&oacute;n amplia de tu negocio.</li>".
	      		"<li>Ubicamos tu negocio en el mapa interactivo.</li>".
	      		"<li>Agreg&aacute; hasta 3 im&aacute;genes de tu negocio.</li>".
      		"</ul>".      		      		
      		"<ul>Full".
	      		"<li>Posicion&aacute; tu negocio Online.</li>".
	      		"<li>Agreg&aacute; una descripci&oacute;n amplia de tu negocio.</li>".
	      		"<li>Ubicamos tu negocio en el mapa interactivo.</li>".	  
	      		"<li>Agreg&aacute; hasta 3 im&aacute;genes de tu negocio.</li>".
	      		"<li>Agregamos tu negocio a la p&aacute;gina principal del sitio para que tenga una mejor llegada a tus clientes.</li>".
      		"</ul>".
      		*/      		
      		"</div>";
      //-----------------------------------------------------
      $output .= "</ul></div></div>";
      
      $output .= "
				    <script type='text/javascript'>
        					//$('#socio').validationEngine('attach');
    				</script>      
      ";
      $output .= $this->load->view('foot_view', $data, true);
      $this->output->set_output($output);
	}
}
?>
